// pages/mine_center/order_lists/order_lists.js
const app = getApp();
var request = require("../../../utils/request.js");
var wx_alert = require("../../../utils/wx_alert.js");
var page = 1;
var state;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selected_tab:0,
    show_have: true        
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    state = options.type;
    //快捷导航跳转
    this.turn = request.turn;
    this.close_modal = request.close_modal;    
    this.setData({
      options: options
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    var that = this;
    var data = {
      state: state,
      limit: 10,
      page: page
    }
    request.request(app.globalData.getOrderList, data, function (res) {
      console.log(res)
      if (res.code == 0) {
        that.setData({
          order_list: res.list,
          img_url: request.url + "/",
          selected_tab: state,
        })
      }
    })      

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  tab_click:function(e){
    console.log(e)
    var that = this;
    page = 1;
    var tab = e.currentTarget.dataset.tab;
    state = tab;
    var data = {
      state: tab,
      limit: 10,
      page: page
    }
    request.request(app.globalData.getOrderList, data, function (res) {
      console.log(res)
      if (res.code == 0) {
        that.setData({
          order_list: res.list,
          selected_tab: tab          
        })
      }
    })    

  },
  list_tap:function(e){
    var order_id = e.currentTarget.dataset.order_id;
    wx.navigateTo({
      url: '/pages/mine_center/order_detail/order_detail?order_id=' + order_id,
    })
  },
  toLower: function () {
    var that = this;
    var order_list = this.data.order_list;
    page ++;
    var data = {
      state: state,
      limit: 10,
      page: page
    }
    request.request(app.globalData.getOrderList, data, function (res) {
      console.log(res)
      if (res.code == 0) {
        for (var i in res.list){
          order_list.push(res.list[i])
        }
        that.setData({
          order_list: order_list,
        })
      }
    })       
  },
  back_money:function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    var order_list = this.data.order_list;
    wx_alert.confirm("您确定要退款吗",function(){
      request.request(app.globalData.updateOrder, { orderId: order_list[index].id, type: 5, }, function (res) {
        
        if (res.code == 0) {
          that.onShow();
        }

      })
    })


  }
})